//
//  Summoner.m
//  TowerDefence
//
//  Created by k3 on 6/8/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "Summoner.h"
#import "PlayScreen.h"
#import "Enemy.h"
#import "iCoreGUIController.h"
@implementation Summoner
@synthesize spriteBarrier;
-(id)initSummmonerWith:(GameLayer*)_gameLayer{
    self=[super initGameObjectWith:_gameLayer];
    spriteBody=[[CCSprite alloc]initWithSpriteFrameName:@"Summoner_01.png"];
    spriteBarrier=[[CCSprite alloc]initWithSpriteFrameName:@"Barrier_Appear_01.png"];
    [spriteBody addChild:spriteBarrier];
    barrier=YES;
    die=NO;
    [self loadSummoner];
    return self;
}

-(void)update:(ccTime)_timeUpdate{
    
    for (int i=0; i<handler.listGameObject.count;i++ ) {
        if ([[handler.listGameObject objectAtIndex:i]isKindOfClass:[Enemy class]]){
            Enemy *enemy=(Enemy*)[handler.listGameObject objectAtIndex:i];
            if (ccpDistance(spriteBody.position, enemy.spriteBody.position)<=spriteBarrier.contentSize.width/2-15) {
                enemy.reachSummoner=YES;
                //fixed
                if (handler.mainGUIController.playerData.life>1){
                    [spriteBarrier runAction:[CCAnimate actionWithAnimation:[self animationBarrierGotHit] restoreOriginalFrame:NO]];
//                    NSLog(@"Life %d",((PlayScreen*)handler).mainGUIController.playerData.life);
                }
                if(handler.mainGUIController.playerData.life==1&&barrier){
                    [spriteBarrier runAction:[CCAnimate actionWithAnimation:[self animationBarrierDissappear] restoreOriginalFrame:NO]];
                    barrier=NO;
                }
                if(((PlayScreen*)handler).mainGUIController.playerData.life<=0 && !die){
                    CCCallFunc *func=[CCCallFunc actionWithTarget:self selector:@selector(die)];
                    [spriteBody runAction:[CCSequence actions:[CCAnimate actionWithAnimation:[self animationSummonerDie] restoreOriginalFrame:NO],func, nil]];
                    break;
                }
                //end fixed
            }
        }
    }
}

-(void)loadSummoner{
//    CCCallFunc *func=[CCCallFunc actionWithTarget:self selector:@selector(barrierAppear)];
    spriteBarrier.position=ccpAdd(spriteBody.position,ccp(15,10));
    [spriteBarrier runAction:[CCAnimate actionWithAnimation:[self animationBarrierAppear] restoreOriginalFrame:NO]];
    [spriteBody runAction:[CCSequence actions:[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:[self animationSummoning] restoreOriginalFrame:NO]],nil]];
}

-(void)barrierAppear{
    [spriteBarrier runAction:[CCAnimate actionWithAnimation:[self animationBarrierAppear] restoreOriginalFrame:NO]];
}

-(CCAnimation*)animationSummoning{
    CCAnimation *animation = [self loadAnimationFromCacheByName:@"Summoner" withCount:21 delay:0.1];
    return animation;
}

-(CCAnimation*)animationBarrierAppear{
    CCAnimation *animation = [self loadAnimationFromCacheByName:@"Barrier_Appear" withCount:33 delay:0.1];
    return animation;
}

-(CCAnimation*)animationBarrierDissappear{
    NSMutableArray *anim=[[NSMutableArray alloc]init];
    for (int i=32;i>0;i--){
        NSString *frameName=[NSString stringWithFormat:@"Barrier_Appear_%d.png",i];
        if (i<10)
            frameName=[NSString stringWithFormat:@"Barrier_Appear_0%d.png",i];
        CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:frameName];
        [anim addObject:frame];
    }
    CCAnimation *animation = [CCAnimation animationWithFrames:anim delay:0.1];
    return animation;
}

-(CCAnimation*)animationBarrierGotHit{
    NSMutableArray *anim=[[NSMutableArray alloc]init];
    for (int i=18;i<27;i++){
        NSString *frameName=[NSString stringWithFormat:@"Barrier_Appear_%d.png",i];
        CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:frameName];
        [anim addObject:frame];
    }
    CCAnimation *animation = [CCAnimation animationWithFrames:anim delay:0.1];
    return animation;
}

-(CCAnimation*)animationSummonerDie{
    CCAnimation *animation = [self loadAnimationFromCacheByName:@"Summoner_Die" withCount:6 delay:0.1];
    return animation;
}

-(void)die{
    die=YES;
    [self stopAllActions];
    [spriteBody removeFromParentAndCleanup:YES];
    [handler removeGameObject:self];
}

@end
